//
//  PicUrlsCollectionView.swift
//  WBDemo
//
//  Created by Jin on 09/05/2018.
//  Copyright © 2018 com.baidu.123. All rights reserved.
//

import UIKit

// MARK: - PicUrlsCollectionView
class PicUrlsCollectionView: UICollectionView {
    
    var picUrls : [URL] = [URL](){
        didSet{
            self.reloadData()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        dataSource = self
    }

}

// MARK: - UICollectionViewDataSource
extension PicUrlsCollectionView:UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PicCell", for: indexPath) as! PicUrlsCollectionViewCell
        
        cell.picUrl = picUrls[indexPath.row]
        return cell
    }
}

// MARK: - PicUrlsCollectionViewCell
class PicUrlsCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var picImageView:UIImageView!
    
    var picUrl : URL? {
        didSet{
            guard let url = picUrl else {
                return
            }
            
            picImageView.sd_setImage(with: url, placeholderImage: nil, options: [], completed: nil)
        }
    }
    
    
}
